package com.braska.hewankurban;

import org.json.JSONArray;
import org.json.JSONObject;

import com.braska.hewankurban.helper.ServiceHelper;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class PeriksaDokterActivity extends Activity {

	EditText nama_periksa, berat_periksa, umur_periksa, cacat_periksa;
	Intent i;
	ServiceHelper srv = new ServiceHelper();
	String id;
	String nama;
	String berat;
	String umur;
	String jenishewan;
	String id_pemilik;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_periksadokter);
		Bundle extras = getIntent().getExtras();
		jenishewan = extras.getString("hewan");
		nama = extras.getString("nama");
		berat = extras.getString("berat");
		umur = extras.getString("umur");
		String cacat = extras.getString("cacat");
		id = extras.getString("id");
		nama_periksa = (EditText) findViewById(R.id.ed_nama);
		berat_periksa = (EditText) findViewById(R.id.ed_berat);
		umur_periksa = (EditText) findViewById(R.id.ed_umur);
		cacat_periksa = (EditText) findViewById(R.id.ed_cacat);
		LinearLayout l = (LinearLayout) findViewById(R.id.L_1);
		TextView jenis = (TextView) findViewById(R.id.jenishewan);
		Button submit = (Button) findViewById(R.id.btn_submit);
		Button cancel = (Button) findViewById(R.id.btn_cancel);

		final CheckBox c = (CheckBox) findViewById(R.id.sehat);

		new idpemiliktask().execute();
		nama_periksa.setText(nama);
		berat_periksa.setText(berat);
		umur_periksa.setText(umur);
		cacat_periksa.setText(cacat);

		i = new Intent(getApplicationContext(), BiodataActivity.class);
		i.putExtra("id", id);
		cancel.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				startActivity(i);
				finish();

			}
		});

		submit.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if (c.isChecked()) {
					new submitktask().execute();
				} else {
					Toast.makeText(getBaseContext(), "Checkbox harus di pilih",
							Toast.LENGTH_LONG).show();
				}

			}
		});
		if (jenishewan.equals("2")) {
			// l_3.setVisibility(View.VISIBLE);
			jenis.setText("Pemeriksaan Kambing");
			submit.setBackgroundColor(Color.parseColor("#FC8F05"));
			cancel.setBackgroundColor(Color.parseColor("#FC8F05"));
			l.setBackgroundColor(Color.parseColor("#FC8F05"));
			
		} else if (jenishewan.equals("1")) {
			jenis.setText("Pemeriksaan Sapi");
			l.setBackgroundColor(Color.parseColor("#18B7C5"));
			submit.setBackgroundColor(Color.parseColor("#18B7C5"));
			cancel.setBackgroundColor(Color.parseColor("#18B7C5"));

		} else if (jenishewan.equals("3")) {

			jenis.setText("Pemeriksaan Domba");
			l.setBackgroundColor(Color.parseColor("#54D34F"));
			submit.setBackgroundColor(Color.parseColor("#54D34F"));
			cancel.setBackgroundColor(Color.parseColor("#54D34F"));

		}

	}

	public class submitktask extends AsyncTask<String, Void, Boolean> {

		ProgressDialog dialog = new ProgressDialog(PeriksaDokterActivity.this);

		protected void onPreExecute() {
			dialog.setMessage(" Loading... ");
			dialog.show();
		}

		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub

			// setList();
			dialog.dismiss();

		}

		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			String srvres;
			try {
				// String srvres = srv.tambahperiksa(id, "1", hewan, u, berat);
				// srvres = srv.tambahperiksa("1", "23", "2", "2", "30");

				srvres = srv.tambahperiksa(id, id_pemilik, jenishewan, umur,
						berat);

				Log.i("1", id);
				Log.i("2", nama);
				Log.i("3", berat);
				Log.i("4", jenishewan);
				Log.i("7 umur", umur);

				if (srvres.equals("1")) {

					Log.d("sukses", srvres);
					i.putExtra("id", "1");
					startActivity(i);
					finish();
				} else {
					Log.d("gagal", srvres);
				}
				Log.i("metura iki tambah e", srvres);

			} catch (Exception e) {
				// TODO: handle exception
				Log.i("haha", e + "");
			}
			return true;
		}

	}

	public class idpemiliktask extends AsyncTask<String, Void, Boolean> {

		ProgressDialog dialog = new ProgressDialog(PeriksaDokterActivity.this);

		protected void onPreExecute() {
			dialog.setMessage(" Loading... ");
			dialog.show();
		}

		@Override
		protected void onPostExecute(Boolean result) {
			// TODO Auto-generated method stub

			// setList();
			dialog.dismiss();

		}

		@Override
		protected Boolean doInBackground(String... params) {
			// TODO Auto-generated method stub
			try {
				String srvres = srv.getidpemilik(nama);
				Log.i("alakazam", srvres);
				try {
					JSONArray ja = new JSONArray("[" + srvres + "]");
					JSONArray innerJSONArray = ja.getJSONArray(0);
					JSONObject json = innerJSONArray.getJSONObject(0);
					Log.d("anunedowoooobangettttt", innerJSONArray.length()
							+ "");

					for (int i = 0; i < innerJSONArray.length(); i++) {
						JSONObject j = innerJSONArray.getJSONObject(i);
						id_pemilik = j.getString("id_pemilik_hewan");

					}

					Log.i("id", id_pemilik);
				} catch (Exception e) {
					// TODO: handle exception
				}
			} catch (Exception e) {
				// TODO: handle exception
			}
			return true;
		}
	}

}
